<?php
namespace App\Edu\Services;

use App\Edu\Models\EduQuestionModel;
use App\Services\CommonBaseService;

class EduQuestionService extends CommonBaseService
{

    public function __construct(EduQuestionModel $model)
    {
        parent::__construct($model);
    }

    /** 
     * 保存题目
     */
    public function saveInfo($data)
    {
        $saveData = [
            'question_title' => $data['question_title'], // 试卷名称
            'cateid' => $data['cateid'], // 试卷分类
            'province_id' => $data['province_id'], // 省份
            'question_type' => $data['question_type'], // 试卷类型
            'question_option' => $data['question_option'], // 试卷选项
            'question_answer' => $data['question_answer'], // 答案
            'question_exp' => $data['question_exp'], // 解析
            'is_paper' => 0,
        ];
        if (empty($data['question_id'])) {
            $questionId = $this->model->insertData($saveData);
        } else {
            $this->model->updateData($saveData, ['question_id' => $data['question_id']]);
            $questionId = $data['question_id'];
        }
        return $questionId;
    }

    /** 
     * 删除试卷
     */
    public function deleteQuestion($id)
    {
        return $this->deleteBy(['question_id' => $id]);
    }

    /** 
     * 启用，禁用
     */
    public function changeQuestionStatus($id)
    {
        $info = $this->findOneBy(['question_id' => $id], 'status');
        return $this->updateBy(['question_id' => $id], ['status' => $info['status'] == 1 ? 0 : 1]);
    }

    /** 
     * 列表
     */
    public function questionList($params)
    {
        $list = $this->model->getList($params, $this->getPageSize($params));
        return $list;
    }

    /** 
     * 详情
     */
    public function QuestionInfo($id, $userId = 0)
    {
        if (!empty($userId)) {
            // 判断是否购买过该试卷
        }
        $info = $this->model->findOneBy(['question_id' => $id]);
        return $info;
    }
}
